﻿using AutoMapper;
using MDT.LifeSewagePlatform.Conmmon;
using MDT.LifeSewagePlatform.InterfaceService;
using MDT.LifeSewagePlatform.Entity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using System.IO;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System.Text;
using Microsoft.Extensions.Configuration;

namespace MDT.LifeSewagePlatform.ServiceInstance.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [ApiController]
    [Route("api/user")]
    [Authorize]
    public class UserController : BaseController
    {
        #region prop and ctor
        [Autowired]
        private IUserService userService { get; set; }

        [Autowired]
        private IMapper mapper { get; set; }
        [Autowired]
        private ILogger<UserController> logger { get; set; }
        private IConfiguration configuration { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public UserController()
        {

        }
        #endregion

        #region method service
        /// <summary>
        /// 用户登录接口
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("login")]
        [AllowAnonymous]
        public async Task<BaseMessage> Login([FromForm] LoginModel model)
        {
            return await userService.Login(model);
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost()]
        [CustomActionFilter(Role = RoleType.Admin)]
        public async Task<BaseMessage> AddUser([FromForm] AddUser user)
        {
         return await userService.Add(user, GetUserID());
        }
        /// <summary>
        /// 获取用户集合
        /// </summary>
        /// <param name="Params"></param>
        /// <returns></returns>
        [HttpGet()]
        [CustomActionFilter(Role = RoleType.Admin)]
        public async Task<BaseMessage> GetUsers([FromQuery] UserParameter Params)
        {
           return await userService.GetUsers(Params);
        }
        /// <summary>
        /// 获取用户通过id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        [CustomActionFilter(Role = RoleType.Admin)]
        public async Task<BaseMessage> GetUser(int id)
        {
           return await userService.FindUserById(id);
        }
        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="id"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        [CustomActionFilter(Role = RoleType.Admin)]
        public async Task<BaseMessage> Update(int id, [FromForm] UpdateUser user)
        {
            return await userService.Update(user, id, GetUserID());
        }
        /// <summary>
        /// 更新用户密码
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpPut("password")]
        [CustomActionFilter(Role = RoleType.Admin)]
        public async Task<BaseMessage> UpdatePassword([FromForm] UpdatePassword password)
        {
            return await userService.UpdatePassword(GetUserID(), password);
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("soft/{id}")]
        [CustomActionFilter(Role = RoleType.Admin)]
        public async Task<BaseMessage> DeleteUser(int id)
        {
            return await userService.SoftDelete(id, GetUserID());
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("info")]
        [CustomActionFilter(Role = RoleType.Child)]
        public async Task<BaseMessage> GetUserInfo()
        {
            return await userService.FindUserById(GetUserID());
        }
        /// <summary>
        /// 恢复已删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("unsoftdelete/{id}")]
        [CustomActionFilter(Role = RoleType.Admin)]
        public async Task<BaseMessage> UnDelete(int id)
        {
            return await userService.UnSoftDelete(id, GetUserID());
        }
        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpGet("code/{phone}")]
        [AllowAnonymous]
        public async Task<BaseMessage> GetCode(string phone)
        {
            return await userService.GetCode(phone);
        }
        /// <summary>
        /// 忘记密码
        /// </summary>
        /// <param name="Params"></param>
        /// <returns></returns>
        [HttpPost("forget")]
        [AllowAnonymous]
        public async Task<BaseMessage> ForgetPassword([FromForm] ForgetPassword Params)
        {
            return await userService.FrogetPassword(Params);
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        [CustomActionFilter(Role = RoleType.Admin)]
        public async Task<BaseMessage> Delete(int id)
        {
            return await userService.Delete(id);
        }
        #endregion
    }
}
